<template>
    <el-dialog
            :title="!dataForm.resourceId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="资源唯一标识" prop="resourceId">
                        <el-input v-model.number="dataForm.resourceId" placeholder="资源唯一标识"></el-input>
                </el-form-item>
                <el-form-item label="所属项目ID（外键关联`app_project_info`）" prop="projectId">
                        <el-input v-model.number="dataForm.projectId" placeholder="所属项目ID（外键关联`app_project_info`）"></el-input>
                </el-form-item>
                <el-form-item label="资源类型（如：人力、设备、材料）" prop="resourceType">
                        <el-input v-model="dataForm.resourceType" placeholder="资源类型（如：人力、设备、材料）"></el-input>
                </el-form-item>
                <el-form-item label="资源名称" prop="resourceName">
                        <el-input v-model="dataForm.resourceName" placeholder="资源名称"></el-input>
                </el-form-item>
                <el-form-item label="资源数量" prop="quantity">
                        <el-input v-model="dataForm.quantity" placeholder="资源数量"></el-input>
                </el-form-item>
                <el-form-item label="资源单位（如：人、台、吨等）" prop="unit">
                        <el-input v-model="dataForm.unit" placeholder="资源单位（如：人、台、吨等）"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            resourceId: null,
            projectId: null,
            resourceType: null,
            resourceName: null,
            quantity: null,
            unit: null
          },
          dataRule: {
            resourceId: [
                        { required: true, message: '资源唯一标识不能为空', trigger: 'blur' }
            ],
            projectId: [
                        { required: true, message: '所属项目ID（外键关联`app_project_info`）不能为空', trigger: 'blur' }
            ],
            resourceType: [
                        { required: true, message: '资源类型（如：人力、设备、材料）不能为空', trigger: 'blur' }
            ],
            resourceName: [
                        { required: true, message: '资源名称不能为空', trigger: 'blur' }
            ],
            quantity: [
                        { required: true, message: '资源数量不能为空', trigger: 'blur' }
            ],
            unit: [
                        { required: true, message: '资源单位（如：人、台、吨等）不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.resourceId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.resourceId = id || 0
            if (this.dataForm.resourceId) {
              this.$http({
                url: this.$http.adornUrl(`/app/project_resource/info/${this.dataForm.resourceId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appprojectresource
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/project_resource/${!this.dataForm.resourceId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>